﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DBBase;

namespace Business
{
    public partial class weiqiReportsetting : DevExpress.XtraEditors.XtraForm
    {
        public weiqiReportsetting()
        {
            InitializeComponent();
        }

        private float X;

        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize <= 0)
                {
                    currentSize = 0.1f;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / X;
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            this.Text = this.Width.ToString() + " " + this.Height.ToString();

        }
        private void weiqiReportsetting_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize); 

            X = this.Width;
            Y = this.Height;


            setTag(this);
            Form1_Resize(new object(), new EventArgs());//x,y可在实例化时赋值,最后这句是新加的，在MDI时有用
            init();

        }
        void init()
        {
            INIfile ini = new INIfile();
            ini.path = "./RepPrint.ini";
            textBox1.Text = ini.IniReadValue("设置", "检测机构名称");
            textBox2.Text = ini.IniReadValue("设置", "行政区代码");
            textBox3.Text = ini.IniReadValue("设置", "检验机构联网顺序号");
            textBox4.Text = ini.IniReadValue("设置", "计量认证证号");


            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from StationInfor");
            DataTable dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                textBox1.Text = dt.Rows[0]["SI_NAME"].ToString().Trim();
                textBox2.Text = dt.Rows[0]["SI_XZQYM"].ToString().Trim();
                textBox3.Text = dt.Rows[0]["SI_LWXH"].ToString().Trim();
                textBox4.Text = dt.Rows[0]["SI_JLRZH"].ToString().Trim();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            INIfile ini = new INIfile();
            ini.path = "./RepPrint.ini";
            ini.IniWriteValue("设置", "检测机构名称", textBox1.Text);
            ini.IniWriteValue("设置", "行政区代码", textBox2.Text);
            ini.IniWriteValue("设置", "检验机构联网顺序号", textBox3.Text);
            ini.IniWriteValue("设置", "计量认证证号", textBox4.Text);
            MessageBox.Show("设置完成");


            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from StationInfor");
            DataTable dt = mquery.GetTable(sql);
            string ssql = "";
            if (dt.Rows.Count > 0)
            {
                ssql = string.Format(@"update StationInfor set 
                                            SI_NAME='{0}',
                                            SI_XZQYM='{1}',
                                            SI_LWXH='{2}',
                                            SI_JLRZH='{3}'",
                                            textBox1.Text,
                                            textBox2.Text,
                                            textBox3.Text,
                                            textBox4.Text
                                            );
            }
            else
            {
                ssql = string.Format(@"insert into StationInfor (
                                    SI_NAME,
                                    SI_XZQYM,
                                    SI_LWXH,
                                    SI_JLRZH)
                                    values (
                                    '{0}',
                                    '{1}',
                                    '{2}',
                                    '{3}',
                                    '{4}')",
                                    textBox1.Text,
                                    textBox2.Text,
                                    textBox3.Text,
                                    textBox4.Text);
            }
            mquery.InsertDB(ssql);

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}